<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

class admin_controller_dashboard extends omnitrix_controller
{
	public function index_action()
	{
		otx_html_add_navtab($this->_('navtab_index_title'), $this->_('navtab_index_desc'), otx_request_uri(), true);
		otx_html_add_breadcrumb($this->_('breadcrumb_index'));
		otx_html_response_title($this->_('breadcrumb_index'));

		$this->_render_system_info();
		$this->_render_backup_list();
		$this->_render_system_paths();
		$this->_render_admin_note();
	}

	protected function _render_system_info()
	{
		$db_size = otx_db_fetch_one('SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = ?', DB_DATABASE);
		$db_version = otx_db_fetch_one('SELECT version()');

		$list_table = otx_html_create_table($this->_('heading_system'), 'table-list');

		$list_table->add_body_row();
		$list_table->set_body_cell(otx_html_strong($this->_('text_server')), array('width' => '20%'));
		$list_table->set_body_cell(PHP_OS . ' / ' . $_SERVER['SERVER_SOFTWARE'], array('width' => '30%'));
		$list_table->set_body_cell(otx_html_strong($this->_('text_cache_size')), array('width' => '20%'));
		$list_table->set_body_cell(otx_string_format_size(otx_file_size(DIR_CACHE)), array('width' => '30%'));

		$list_table->add_body_row();
		$list_table->set_body_cell(otx_html_strong($this->_('text_database')));
		$list_table->set_body_cell(ucfirst(DB_TYPE) . ' ' . $db_version . ' (' . otx_string_format_size($db_size) . ')');
		$list_table->set_body_cell(otx_html_strong($this->_('text_backup_size')));
		$list_table->set_body_cell(otx_string_format_size(otx_file_size(DIR_BACKUP)));

		$list_table->add_body_row();
		$list_table->set_body_cell(otx_html_strong($this->_('text_php_version')));
		$list_table->set_body_cell(PHP_VERSION);
		$list_table->set_body_cell(otx_html_strong($this->_('text_upload_limit')));
		$list_table->set_body_cell(@ini_get('upload_max_filesize'));

		$list_table->add_body_row();
		$list_table->set_body_cell(otx_html_strong($this->_('text_omnitrix_version')));
		$list_table->set_body_cell(OMNITRIX_VERSION);
		$list_table->set_body_cell(otx_html_strong($this->_('text_post_limit')));
		$list_table->set_body_cell(@ini_get('post_max_size'));
	}

	protected function _render_system_paths()
	{
		$list_table = otx_html_create_table($this->_('heading_system_paths'), 'table-list');
		$list_table->set_head_cell($this->_('column_name'), array('width' => '25%'));
		$list_table->set_head_cell($this->_('column_path'));
		$list_table->set_head_cell($this->_('column_permission'), array('width' => '20%'));

		$system_paths = array(
			array(
				'name' => $this->_('text_dir_install'),
				'path' => DIR_ROOT,
				'mode' => true
			),
			array(
				'name' => $this->_('text_dir_system'),
				'path' => DIR_SYSTEM,
				'mode' => true
			),
			array(
				'name' => $this->_('text_dir_modules'),
				'path' => DIR_MODULES,
				'mode' => true
			),
			array(
				'name' => $this->_('text_dir_plugins'),
				'path' => DIR_PLUGINS,
				'mode' => true
			),
			array(
				'name' => $this->_('text_dir_themes'),
				'path' => DIR_THEMES,
				'mode' => true
			),
			array(
				'name' => $this->_('text_dir_languages'),
				'path' => DIR_LANGUAGES,
				'mode' => true
			),
			array(
				'name' => $this->_('text_dir_cache'),
				'path' => DIR_CACHE,
				'mode' => true
			),
			array(
				'name' => $this->_('text_dir_upload'),
				'path' => DIR_UPLOAD,
				'mode' => true
			),
			array(
				'name' => $this->_('text_dir_backup'),
				'path' => DIR_BACKUP,
				'mode' => true
			),
			array(
				'name' => $this->_('text_file_config'),
				'path' => DIR_ROOT . 'config.php',
				'mode' => true
			)
		);

		foreach ($system_paths as $system_path) {
			$list_table->add_body_row();
			$list_table->set_body_cell(otx_html_strong($system_path['name']));
			$list_table->set_body_cell($system_path['path']);
			$list_table->set_body_cell(otx_html_span(is_writable($system_path['path']) ? $this->_('text_writable') : $this->_('text_notwritable'), array('class' => is_writable($system_path['path']) === $system_path['mode'] ? 'green' : 'red')));
		}
	}

	protected function _render_backup_list()
	{
		$backup_list = array();
		$backup_files = glob(DIR_BACKUP . 'backup.*');

		if ($backup_files) {
			foreach ($backup_files as $file_name) {
				$base_name = basename(basename($file_name, '.sql'), '.sql.gz');
				$file_time = substr(strrchr($base_name, '.'), 1);
				$backup_list[] = array(
						'name' => basename($file_name),
						'time' => $file_time
				);
			}
		}

		$list_table = otx_html_create_table($this->_('heading_backup_list'), 'table-list');
		$list_table->set_head_cell($this->_('column_file'));
		$list_table->set_head_cell($this->_('column_time'), array('class' => 'center', 'width' => 200));

		if ($backup_list) {
			foreach ($backup_list as $row) {
				$list_table->add_body_row();
				$list_table->set_body_cell(otx_html_link(otx_html_strong($row['name']), array('href' => otx_router_url('download', 'backup', null, array('backup_name' => $row['name'])))));
				$list_table->set_body_cell(otx_string_format_datetime($row['time']), array('class' => 'center'));
			};
		} else {
			$list_table->add_body_row();
			$list_table->set_body_cell($this->_('text_no_results'), array('colspan' => 2));
		}
	}

	protected function _render_admin_note()
	{
		if (otx_request_is_post()) {
			$admin_note = otx_param_string('admin_note');
			otx_config_write('system_admin_note', $admin_note);
			otx_cache_delete('config');
		} else {
			$admin_note = otx_config_string('system_admin_note');
		}

		$form_wrapper = otx_html_create_wrapper(null, null, $this->_('button_save'));
		$form_table = $form_wrapper->create_table($this->_('heading_admin_note'), 'table-list');
		$form_table->set_head_cell($this->_('entry_admin_note'));
		$form_table->set_body_cell(otx_html_form_textarea('admin_note', $admin_note, array('style' => 'width: 99.3%; height: 200px;')));
	}
}